﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Expressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class AllApplication : System.Web.UI.Page
    {
        public int RecordsNumber { get; set; }
        public int PageCount { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["PageIndex"] = 0;
                BindApplicationGridview();
            }
        }

        protected void viewLinkButton_click(object sender, EventArgs e)
        {
            string applicationid = ((LinkButton)sender).CommandArgument;
            Response.Redirect(string.Format("OriginalApplicationDetail.aspx?{0}={1}", Constants.QUERYSTR_APPLICATION_ID, applicationid));
        }

        private void BindApplicationGridview()
        {
            try
            {
                ApplicationService service = new ApplicationService();
                List<ApplicationFormHistory> dataset = new List<ApplicationFormHistory>();

                List<ApplicationFormHistory> originalApplications = service.GetAllOriginalApplicationPaging(keyWordTextBox.Text,
                    Convert.ToInt32(Session["PageIndex"]), 20);
                foreach (ApplicationFormHistory originalApplication in originalApplications)
                {
                    ApplicationForm application = service.GetApplicationByRunningNO(originalApplication.RunningNumber);
                    if (application != null)
                    {
                        originalApplication.Status = application.Status;
                        originalApplication.ApplicationCode = application.ApplicationCode;
                        originalApplication.ReceiveDateTime = application.ReceiveDateTime;
                    }
                    dataset.Add(originalApplication);
                }

                RecordsNumber = service.GetCountOfAllOriginalApplication(keyWordTextBox.Text);
                PageCount = RecordsNumber / 20;
                Session["PageCount"] = RecordsNumber / 20;
                originalGridView.DataSource = dataset;
                originalGridView.PageIndex = 0;
                originalGridView.DataBind();
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "AllApplication::BindApplicationGridview");
                ClientScript.RegisterStartupScript(this.GetType(), "tip", string.Format("alert('信息绑定发生错误，请刷新页面重试，如持续出现该错误请截图保存该错误信息并联系管理员！错误原因：{0}');", ex.Message), true);
            }
        }

        protected void originalGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
        }

        protected void originalGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.DataItem != null)
                {
                    e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                    ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
                }
                if (e.Row.RowType == DataControlRowType.Pager)
                {
                    DropDownList pageDDL = e.Row.FindControl("pageDDL") as DropDownList;
                    for (int index = 1; index <= PageCount; index++)
                    {
                        pageDDL.Items.Add(index.ToString());
                    }
                    pageDDL.SelectedIndex = Convert.ToInt32(Session["PageIndex"]);
                }
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "AllApplication::originalGridView_RowDataBound");
                ClientScript.RegisterStartupScript(this.GetType(), "tip", string.Format("alert('信息绑定发生错误，请刷新页面重试，如持续出现该错误请截图保存该错误信息并联系管理员！错误原因：{0}');", ex.Message), true);
            }
        }

        protected void searchLinkButton_Click(object sender, EventArgs e)
        {
            BindApplicationGridview();
        }

        protected void resetLinkButton_Click(object sender, EventArgs e)
        {
            keyWordTextBox.Text = "";
            BindApplicationGridview();
        }

        protected void pageDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList pageDDL = sender as DropDownList;
            Session["PageIndex"] = pageDDL.SelectedIndex;
            BindApplicationGridview();
            
        }

        protected void originalGridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                ApplicationService service = new ApplicationService();
                List<ApplicationFormHistory> dataset = new List<ApplicationFormHistory>();

                List<ApplicationFormHistory> originalApplications = service.GetAllOriginalApplication(keyWordTextBox.Text);
                foreach (ApplicationFormHistory originalApplication in originalApplications)
                {
                    ApplicationForm application = service.GetApplicationByRunningNO(originalApplication.RunningNumber);
                    if (application != null)
                    {
                        originalApplication.Status = application.Status;
                        originalApplication.ApplicationCode = application.ApplicationCode;
                        originalApplication.ReceiveDateTime = application.ReceiveDateTime;
                    }
                    dataset.Add(originalApplication);
                }

                RecordsNumber = dataset.Count;

                string sortDirection = "asc";
                if (Session["OrderDirection"] != null)
                {
                    sortDirection = Session["OrderDirection"].ToString();
                    if (sortDirection.Equals("asc"))
                    {
                        Session["OrderDirection"] = "desc";
                    }
                    else
                    {
                        Session["OrderDirection"] = "asc";
                    }
                }
                else
                {
                    Session["OrderDirection"] = "asc";
                }

                originalGridView.DataSource = dataset.AsQueryable().OrderBy(string.Format("{0} {1}", e.SortExpression, sortDirection));
                originalGridView.DataBind();
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "AllApplication::originalGridView_Sorting");
                ClientScript.RegisterStartupScript(this.GetType(), "tip", string.Format("alert('信息绑定发生错误，请刷新页面重试，如持续出现该错误请截图保存该错误信息并联系管理员！错误原因：{0}');", ex.Message), true);
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Session["PageIndex"] = 0;
            BindApplicationGridview();
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            try
            {
                if (Session["PageIndex"] != null)
                {
                    if (!Session["PageIndex"].Equals("0"))
                    {
                        Session["PageIndex"] = Convert.ToInt32(Session["PageIndex"]) - 1;
                    }
                }
                else
                {
                    Session["PageIndex"] = 0;
                }

                BindApplicationGridview();
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "AllApplication::LinkButton2_Click");
                ClientScript.RegisterStartupScript(this.GetType(), "tip", string.Format("alert('信息绑定发生错误，请刷新页面重试，如持续出现该错误请截图保存该错误信息并联系管理员！错误原因：{0}');", ex.Message), true);
            }
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            try
            {
                if (Session["PageIndex"] != null)
                {
                    if (!Session["PageIndex"].Equals(Convert.ToInt32(Session["PageCount"]) - 1))
                    {
                        Session["PageIndex"] = Convert.ToInt32(Session["PageIndex"]) + 1;
                    }
                }
                else
                {
                    Session["PageIndex"] = 0;
                }
                BindApplicationGridview();
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "AllApplication::LinkButton3_Click");
                ClientScript.RegisterStartupScript(this.GetType(), "tip", string.Format("alert('信息绑定发生错误，请刷新页面重试，如持续出现该错误请截图保存该错误信息并联系管理员！错误原因：{0}');", ex.Message), true);
            }
        }

        protected void LinkButton4_Click(object sender, EventArgs e)
        {
            try
            {
                if (Session["PageIndex"] != null)
                {
                    Session["PageIndex"] = Convert.ToInt32(Session["PageCount"]) - 1;
                }
                else
                {
                    Session["PageIndex"] = 0;
                }
                BindApplicationGridview();
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "AllApplication::LinkButton4_Click");
                ClientScript.RegisterStartupScript(this.GetType(), "tip", string.Format("alert('信息绑定发生错误，请刷新页面重试，如持续出现该错误请截图保存该错误信息并联系管理员！错误原因：{0}');", ex.Message), true);
            }
        }
    }
}
